package com.huatec.controller;


import com.huatec.entity.StuClass;
import com.huatec.service.StuClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//标注这是一个controller
@Controller
//要映射哪个模块 格式-->url:/模块/资源/{id}/细分，比如说-->/StudentClass/list
@RequestMapping("/StudentClass")
public class StuClassController {
    @Autowired
    private StuClassService classService;

    //分页查询学生班级
    //映射具体的action @PathVariable("offset") int offset, @PathVariable("limit") int limit,
    @RequestMapping(value = "/list/{offset}/{limit}", method = RequestMethod.GET)
    public String list(@PathVariable("offset") int offset, @PathVariable("limit") int limit, Model model) {
        List<StuClass> list = classService.queryByPage(offset, limit);
        model.addAttribute("list", list);
        //因为前面已经配置过了，spring会自动去 /WEB-INF/jsp/list.jsp 这个目录下去寻找jsp页面
        return "list";
    }

    //详情页面
    @RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
    public String detail(@PathVariable("id") int id, Model model) {
        if (id > 0) {
            StuClass sc = classService.queryById(id);
            model.addAttribute("obj", sc);
            return "detail";
        } else {
            return "redirect:/StudentClass/list";
        }

    }

    //删除班级
    @RequestMapping(value = "/{id}/delClass", method = RequestMethod.GET)
    public String delClass(@PathVariable("id") int id, Model model) {
        if (id > 0) {
            int count = classService.delStuClass(id);
            if (count > 0) {
                model.addAttribute("result", 0);
                model.addAttribute("msg", "ok");
            } else {
                model.addAttribute("result", 500);
                model.addAttribute("msg", "删除失败");
            }
        } else {
            model.addAttribute("result", 501);
            model.addAttribute("msg", "id有误");
        }
        return "delClass";
    }

    //新增班级
    @RequestMapping(value = "/{className}/addClass", method = RequestMethod.GET)
    public String addClass(@PathVariable("className") String className, Model model) {
        if (className != null && className.trim().length() > 0) {
            StuClass stuClass = new StuClass();
            stuClass.setClassName(className);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            stuClass.setCreateTime(df.format(new Date()));//new Date()为获取当前系统时间
            int count = classService.addStuClass(stuClass);
            if (count > 0) {
                model.addAttribute("result", 0);
                model.addAttribute("msg", "ok");
            } else {
                model.addAttribute("result", 500);
                model.addAttribute("msg", "添加失败");
            }
        } else {
            model.addAttribute("result", 501);
            model.addAttribute("msg", "参数有误");
        }
        return "addClass";
    }

    //修改班级
    @RequestMapping(value = "/{className}/modifyClass", method = RequestMethod.GET)
    public String modifyClass(@PathVariable("className") String className, Model model) {
        if (className != null && className.trim().length() > 0) {
            StuClass stuClass = new StuClass();
            stuClass.setClassName(className);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            stuClass.setCreateTime(df.format(new Date()));//new Date()为获取当前系统时间
            int count = classService.modifyStuClass(stuClass);
            if (count > 0) {
                model.addAttribute("result", 0);
                model.addAttribute("msg", "ok");
            } else {
                model.addAttribute("result", 500);
                model.addAttribute("msg", "添加失败");
            }
        } else {
            model.addAttribute("result", 501);
            model.addAttribute("msg", "参数有误");
        }
        return "modifyClass";
    }
}
